package com.ansion.basedb.dao.impl;


import com.ansion.basedb.dao.GroupDao;
import com.ansion.basedb.dao.model.BaseModel;
import com.ansion.basedb.dao.model.GroupModel;
import com.ansion.basedb.database.DBOperateAsyncListener;
import com.ansion.basedb.database.DatabaseOptionType;
import com.ansion.basedb.database.IDatabaseManager;
import com.ansion.basedb.storage.StorageSdk;
import com.ansion.basedb.util.UserBroadcastUtils;

import java.util.List;

/**
 * Created by Andre on 23-12-27.
 */
public class GroupLogicDaoImpl implements GroupDao {


	@Override
	public void clearCache() {

	}

	@Override
	public List<GroupModel> loadAllGroups() {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return null;
		}
		String selection = null;
		String[] selectionArgs = null;
		List<GroupModel> groupList = mainDB.select(GroupModel.class, null, selection,
				selectionArgs, null, null, null, null);

		return groupList;
	}



	public GroupModel getGroup(String uid){
		GroupModel mGroupModel = null;
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return mGroupModel;
		}
		String selection = GroupModel.kColumnName_Group_Id + "=?";
		String[] selectionArgs = new String[]{uid};

		List<GroupModel> mGroupModelList = mainDB.select(GroupModel.class, null, selection,
				selectionArgs, null, null, null, null);
		if (null != mGroupModelList && mGroupModelList.size() > 0) {
			mGroupModel = mGroupModelList.get(0);
		}


		return mGroupModel;
	}


	@Override
	public void saveGroup(final GroupModel mGroupModel) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB || mGroupModel == null) {
			return;
		}
		mainDB.replace(GroupModel.class, mGroupModel, new DBOperateAsyncListener() {

			@Override
			public <T extends BaseModel> void onPostExecute(
					DatabaseOptionType arg0, Class<T> arg1, List<T> arg2,
					List<T> arg3) {
				UserBroadcastUtils.sendGroupChangeIntent(UserBroadcastUtils.kDAOCategory_RowReplace, mGroupModel);
			}

		});
	}


	@Override
	public void saveGroup(final List<GroupModel> groupList,
						  final DBOperationCallback callback) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB || groupList == null || groupList.size() == 0) {
			return;
		}
		mainDB.replace(GroupModel.class, groupList,
				new DBOperateAsyncListener() {

					@Override
					public <T extends BaseModel> void onPostExecute(
							DatabaseOptionType arg0, Class<T> arg1,
							List<T> arg2, List<T> arg3) {
						if (null != callback) {
							callback.onFinished();
						}
                        UserBroadcastUtils.sendGroupChangeIntent(UserBroadcastUtils.kDAOCategory_RowReplace, groupList);
					}
				}

		);
	}

	@Override
	public void deleteGroup(String gid) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return;
		}
		GroupModel groupModel = getGroup(gid);
		String whereClause = GroupModel.kColumnName_Group_Id + "=?";
		String[] whereArgs = new String[]{String.valueOf(gid)};

		mainDB.delete(GroupModel.class, whereClause, whereArgs);
		UserBroadcastUtils.sendGroupChangeIntent(UserBroadcastUtils.kDAOCategory_RowRemove, groupModel);
	}

	@Override
	public GroupModel getGroupModel(String uid) {
		return getGroup(uid);
	}

}
